import {createSlice, PayloadAction} from "@reduxjs/toolkit";
import { RootState } from "../store";
import {
    basicInitialState,
    jobIntentInitialState,
    eduInitialState,
    projInitialState,
    workInitialState,
    avatarInitialState
} from "../../redux";
import {orderInitialState} from "../order";

export type ResumeCenterList = (
    {
        basic: typeof basicInitialState,
        avatar: string,
        eduExperience: typeof eduInitialState,
        jobIntent: typeof jobIntentInitialState,
        workInfo: typeof workInitialState,
        projectInfoList: typeof projInitialState,
        skills: string,
        selfEvaluation: string,
        imgSrc: string,
        order: typeof orderInitialState
    }
    & {
        id: string,
        title: string,
        updateDate: number,
        createDate: number,
    }
    )[]


export const resumeImgSrc = '/pic/resume.jfif';


export const initialState: ResumeCenterList = [
    {
        id: 'r00001',
        title: '我的简历',
        updateDate: 0,
        createDate: 0,
        basic: basicInitialState,
        eduExperience: eduInitialState,
        avatar: avatarInitialState,
        skills: '',
        selfEvaluation: '',
        projectInfoList: projInitialState,
        jobIntent: jobIntentInitialState,
        workInfo: workInitialState,
        imgSrc: resumeImgSrc,
        order: orderInitialState
    }
]



const resumeCenterSlice = createSlice({
    name: 'resumeCenter',
    initialState,
    reducers: {
        addResume: (state , action: PayloadAction<{title: string}>) => {
            let newResume = {
                id: '',
                title: '',
                updateDate: 0,
                createDate: 0,
                basic: basicInitialState,
                eduExperience: eduInitialState,
                avatar: avatarInitialState,
                skills: '',
                selfEvaluation: '',
                projectInfoList: projInitialState,
                jobIntent: jobIntentInitialState,
                workInfo: workInitialState,
                imgSrc: resumeImgSrc,
                order: orderInitialState
            }
            let now = + new Date()
            newResume.id = now + '_' + (Math.random() * 10000).toString()
            newResume.createDate = now
            newResume.title = action.payload.title
            state.push(newResume)
        },
        deleteResumeById: (state, action: PayloadAction<string | string[]>) =>  {
            if (Array.isArray(action.payload)) {
                return state.filter(resume => !action.payload.includes(resume.id))
            } else {
                state.splice(
                    state.findIndex(d => d.id === action.payload),
                    1
                )
            }
        },
        updateResume: (state, action: PayloadAction<ResumeCenterList[0]>) => {
            let updateId = action.payload.id;
            state[state.findIndex(d => d.id === updateId)] = action.payload
        }
    }
})

export default resumeCenterSlice.reducer

export const { addResume, deleteResumeById, updateResume } = resumeCenterSlice.actions;

export const getResumeCenterList = (state: RootState) => state.resumeCenter


